﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.UI;
using Hr.BLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HR.Areas.li_jialong.Controllers.Attendance
{
    public class AttendanceController : ApiController
    {
        //导入BLL层
        protected AttendanceService Attendance = new AttendanceService();
        int code = 0;//0是成功，1是失败
        string msg = "success";//信息
        #region 考勤管理API
        /// <summary>
        /// 获取所考勤信息API
        /// </summary>
        /// <returns>考勤信息，Json数据</returns>
        [HttpGet]
        public JObject GetAttendanceList(int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attendance.GetAttendanceList(page,limit)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 添加修改数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateAttendanceList()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Attendance_List r = JsonHelper.DeserializeJsonToObject<Attendance_List>(RequestForm["AttendanceInfo"]);
            string type = RequestForm["Type"];
            int pk_User =int.Parse(RequestForm["pk_User"].ToString());
            if (type.Equals("Edit"))
            {
                Attendance_List SelRo = Attendance.GetAttendance(r.pk_Attendance);
                msg = "更新成功";
                if (SelRo == null)
                {
                    code = 1;
                    msg = "考勤信息不存在";
                }
                else
                {
                    if (ModelHelper.ReflectModel(r, SelRo))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        string ip = ((System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                        r.IPAddress = ip;
                        if (!Attendance.UpdateAttendance(r, type, pk_User))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                msg = "添加成功";
                string ip = ((System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                r.IPAddress = ip;
                if (!Attendance.UpdateAttendance(r, type, pk_User))
                {
                    code = 1;
                    msg = "添加失败";
                }


            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 根据考勤ID查询单个角色
        /// </summary>
        [HttpGet]
        public JObject GetAttendanceInfo(int pk_Attendance)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attendance.GetAttendance(pk_Attendance)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据考勤周报表ID查询单个
        /// </summary>
        [HttpGet]
        public JObject GetAttendanceFullName(int pk_Attendance)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attendance.GetAttendanceFullName(pk_Attendance)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取类型数据
        /// </summary>
        /// <returns>考勤信息，Json数据</returns>
        [HttpGet]
        public JObject GetTypeList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attendance.GetTypeList()},
                {"data2",Attendance.GetStatusList() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 根据FullName查询Attendance_List表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject FNameSelectAttendanceList(int page, int limit, string Description)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Attendance.FNameSelectAttendanceList(page,limit,Description) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 删除考勤管理
        /// </summary>
        /// <param name="pk_Name"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject DelAttendancedata()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Attendance = int.Parse(RequestForm["pk_Attendance"].ToString());
            Attendance_List r = Attendance.GetAttendance(pk_Attendance);
            msg = "删除成功";
            if (r != null)
            {
                if (!Attendance.DelAttendancedata(pk_Attendance))
                {
                    //失败
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "薪资不存在";
            }

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 考勤周报表
        [HttpPost]
        public JObject GetWeeklyList()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            DateTime StartTime = DateTime.Parse(RequestForm["StartTime"].ToString());
            DateTime EndTime = DateTime.Parse(RequestForm["EndTime"].ToString());
            string Term = RequestForm["Term"];
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attendance.GetWeeklyList(StartTime,EndTime,Term)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 考勤月报表
        [HttpPost]
        public JObject GetmonthlyList()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            DateTime StartTime = DateTime.Parse(RequestForm["StartTime"].ToString());
            string Term = RequestForm["Term"];
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Attendance.GetmonthlyList(StartTime,Term)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion
    }
}
